package cn.itcast.crm.service.Impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerRepository customerRepository;
	
	@Override
	public List<Customer> findNoAssociationCustomer() {
		
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasAssociationFixedAreaCustomer(String fixedAreaId) {
		
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associationCustomerToFixedArea(String customerIdStr, String fixedAreaId) {
		customerRepository.clearFixedAreaId(fixedAreaId);
		if(StringUtils.isBlank(customerIdStr)){
			return;
		}
		String[] customerArray = customerIdStr.split(",");
		for (String str : customerArray) {
			Integer id = Integer.parseInt(str);
			customerRepository.updateFixedArea(fixedAreaId,id);
		}
	}

	@Override
	public void regist(Customer customer) {
		
		customerRepository.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		Customer customer = customerRepository.findByTelephone(telephone);
		System.out.println(customer.toString()+"--------------------------------");
		return customer;
	}

	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
		
	}

	@Override
	public Customer login(String telephone, String password) {
		
		return customerRepository.findByTelephoneAndPassword(telephone,password);
	}

	@Override
	public String findFixedAreaIdByAddress(String address) {
		
		return customerRepository.findFixedAreaIdByAddress(address);
		
	}

}
